﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace JotCMS
{
    public interface ICache
    {
        T Get<T>(string key);
        void Add(string key, object value, int CacheDuration);
        bool Exists(string key);
        void Remove(string key);
    }

    public class AspNetCache : ICache
    {
        public System.Web.Caching.Cache _cache { get; set; }

        public AspNetCache(System.Web.Caching.Cache c)
        {
            _cache = c;
        }

        public T Get<T>(string key)
        {
            return (T)_cache[key];
        }

        public void Add(string key, object value, int CacheDuration)
        {
            _cache.Insert(key, value, null, DateTime.Now.AddMinutes(CacheDuration), TimeSpan.Zero);
        }

        public bool Exists(string key)
        {
            if (_cache == null)
                return false;

            return _cache.Get(key) != null;
        }
        
        public void Remove(string key)
        {
            if (_cache != null)
                _cache.Remove(key);
        }
    }
}
